package com.yunquan.ohana.ui.weather

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.google.gson.Gson
import com.yunquan.ohana.R
import com.yunquan.ohana.BR
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.ui.BaseViewModel
import com.yunquan.ohana.entity.Daily
import com.yunquan.ohana.entity.Hourly
import com.yunquan.ohana.entity.WeatherEntity
import com.yunquan.ohana.http.BaseHttp
import com.yunquan.ohana.http.observer.BaseObserver
import com.yunquan.ohana.http.transformer.CommonTransformer
import com.yunquan.ohana.ui.weather.search.WeatherSearchActivity
import me.tatarka.bindingcollectionadapter2.ItemBinding
import okhttp3.RequestBody.Companion.toRequestBody
import retrofit2.http.Query

class WeatherViewModel(application: Application) : BaseViewModel(application) {

    val city = ObservableField<String>()
    val tempMax = ObservableField<String>()
    val tempMin = ObservableField<String>()
    val temp = ObservableField<String>()
    val weatherName = ObservableField<String>()
    val weatherIcon = ObservableField<Int>()
    val weatherBackground = ObservableField<Int>()
    val windSpeed = ObservableField<String>()
    val uvIndex = ObservableField<String>()
    val pressure = ObservableField<String>()
    val pop = ObservableField<String>()

    val dayItems = ObservableArrayList<WeatherDayItemViewModel>()
    val dayItemBinding = ItemBinding.of<WeatherDayItemViewModel>(
        BR.weatherDayItemViewModel,
        R.layout.item_weather_day,
    )

    val hourItems = ObservableArrayList<WeatherHourItemViewModel>()
    val hourItemBinding = ItemBinding.of<WeatherHourItemViewModel>(
        BR.weatherHourItemViewModel,
        R.layout.item_weather_hour,
    )

    val onCityClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val bundle = Bundle()
            bundle.putString("city", city.get())
            startActivityResult(WeatherSearchActivity::class.java, bundle)
        }
    })

    fun setCurrentWeather(daily: Daily, hourly: Hourly) {
        tempMax.set("${daily.tempMax}℃")
        tempMin.set("/${daily.tempMin}℃")
        temp.set("${hourly.temp}℃")
        weatherName.set(hourly.text)
        weatherIcon.set(hourly.getWeatherRes())
        weatherBackground.set(hourly.getBackgroundRes())
        windSpeed.set("${hourly.windSpeed} Km/H")
        uvIndex.set("${daily.uvIndex} Moderate")
        pressure.set("${hourly.pressure} Mm")
        pop.set("${hourly.pop}%")
    }

    fun getWeatherData(location: String, lang: String) {
        BaseHttp.api.getHourlyAndDaily(location, lang)
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<WeatherEntity>() {
                override fun onSuccess(result: WeatherEntity?) {
                    super.onSuccess(result)
                    dayItems.clear()
                    hourItems.clear()
                    result?.hourly?.forEach { hourly ->
                        hourItems.add(WeatherHourItemViewModel(this@WeatherViewModel, hourly))
                    }
                    result?.daily?.forEach { daily ->
                        dayItems.add(WeatherDayItemViewModel(this@WeatherViewModel, daily))
                    }
                    if (result?.daily?.isNotEmpty() == true && result.hourly?.isNotEmpty() == true) {
                        setCurrentWeather(result.daily!!.first(), result.hourly!!.first())
                    }
                }

                override fun onComplete() {
                    super.onComplete()
                    hideLoading()
                }
            })
    }
}